# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest,chi2

df = pd.read_csv('telecom_test.csv',encoding='utf-8') #读取测试集
# print(df.info())
print("edu_class的众数是：",df['edu_class'].mode().loc[0]) #题1：取众数


def jude_null(df,value): #题2：筛选是否有空值？
    if df[df[value].isna() ==True].shape[0]:
        print(value,"有空值！")
    else:
        print(value,"无空值！")
jude_null(df,value='prom')
jude_null(df,value='incomeCode')


df_train = pd.read_csv('telecom_train.csv',encoding='utf-8')  #读取训练集
df2 = df_train.groupby(by=['gender','churn']).size()
df2 = df2.reset_index(name='counts')


def rates(df,flag): #题3
    df_new = df[df['gender'] == flag]
    rates = df_new[df_new['churn'] == 1] / df_new.sum()
    return rates
print(rates(df2,flag=0))  #女性，离网率
print(rates(df2,flag=1))  #男性，离网率


df4 = df_train[['gender', 'edu_class', 'feton', 'prom', 'posPlanChange', 'curPlan', 'call_10086']]
# print(df4.corr()['churn'])
x = df4.values
y =df_train['churn'].values
model = SelectKBest(chi2,k=3)
model.fit(x,y)
print('得分',model.scores_)
print('p值',model.pvalues_)


print( "频数:",df_train[(df_train['feton'] ==1) & (df_train['call_10086'] ==1)].shape[0])  #题5频数


log_duration = df_train['duration'].apply(lambda x: np.emath.log(x))  #题6取log
print(pd.value_counts(pd.cut(log_duration,4)))
df6 = log_duration.describe()
print("四分位距：",df6.loc['75%']- df6.loc['25%'])

df8 = df_train[['churn','duration']]

df8.index =pd.cut(df8['duration'],range(0,72,4)) #离散
df8_1 = df8['churn']

df8_2 = df8_1.reset_index().groupby(by=['duration','churn']).size()
df8_3 =df8_2.reset_index(name='counts')
print(df8_3['counts'].loc[2]/(df8_3['counts'].loc[2]+df8_3['counts'].loc[1]))  #离网率